﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MebAppZip
{
    public class ZipHelper
    {
        public static void ZipFiles(string dir, string[] files, string outputFile)
        {
            var tempFile = Path.GetTempFileName();
            using (ZipArchive archive = ZipFile.Open(tempFile, ZipArchiveMode.Update))
            {
                foreach (var file in files)
                {
                    var relativePath = file.Replace(dir, "").TrimStart('/', '\\');
                    var entry = archive.CreateEntry(relativePath);
                    using (var stream = entry.Open())
                    {
                        using (var fs = File.OpenRead(file)) { fs.CopyTo(stream); }
                    }
                }
            }
            if (File.Exists(outputFile))
            {
                File.Copy(tempFile, outputFile, true);
                File.Delete(tempFile);
            }
            else
            {
                File.Move(tempFile, outputFile);
            }
        }
    }
}
